/**
 * OWASP GoatDroid Project
 * 
 * This file is part of the Open Web Application Security Project (OWASP)
 * GoatDroid project. For details, please see
 * https://www.owasp.org/index.php/Projects/OWASP_GoatDroid_Project
 *
 * Copyright (c) 2011 - The OWASP Foundation
 * 
 * GoatDroid is published by OWASP under the GPLv3 license. You should read and accept the
 * LICENSE before you use, modify, and/or redistribute this software.
 * 
 * @author Jack Mannino, nVisium Security (https://www.nvisiumsecurity.com)
 * @created 2011
 */
package org.owasp.goatdroid.gui.view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextPane;
import java.awt.Point;
import java.awt.Dimension;
import java.io.IOException;
import java.net.SocketException;
import javax.swing.JButton;

import org.owasp.goatdroid.gui.emulator.Emulator;

public class SendLocationFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLatitudeLabel = null;
	private JLabel jLongitudeLabel = null;
	private JTextPane jLatitudeTextPane = null;
	private JTextPane jLongitudeTextPane = null;
	private JButton jSendLocationButton = null;

	/**
	 * This is the default constructor
	 */
	public SendLocationFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Send Location");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLongitudeLabel = new JLabel();
			jLongitudeLabel.setBounds(new Rectangle(49, 78, 64, 16));
			jLongitudeLabel.setText("Longitude");
			jLatitudeLabel = new JLabel();
			jLatitudeLabel.setBounds(new Rectangle(49, 32, 63, 16));
			jLatitudeLabel.setText("Latitude");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLatitudeLabel, null);
			jContentPane.add(jLongitudeLabel, null);
			jContentPane.add(getJLatitudeTextPane(), null);
			jContentPane.add(getJLongitudeTextPane(), null);
			jContentPane.add(getJSendLocationButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jLatitudeTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJLatitudeTextPane() {
		if (jLatitudeTextPane == null) {
			jLatitudeTextPane = new JTextPane();
			jLatitudeTextPane.setSize(new Dimension(101, 16));
			jLatitudeTextPane.setLocation(new Point(140, 32));
		}
		return jLatitudeTextPane;
	}

	/**
	 * This method initializes jLongitudeTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getJLongitudeTextPane() {
		if (jLongitudeTextPane == null) {
			jLongitudeTextPane = new JTextPane();
			jLongitudeTextPane.setLocation(new Point(140, 78));
			jLongitudeTextPane.setSize(new Dimension(101, 16));
		}
		return jLongitudeTextPane;
	}

	/**
	 * This method initializes jSendLocationButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJSendLocationButton() {
		if (jSendLocationButton == null) {
			jSendLocationButton = new JButton();
			jSendLocationButton.setText("Send");
			jSendLocationButton.setLocation(new Point(140, 124));
			jSendLocationButton.setSize(new Dimension(75, 29));
			jSendLocationButton
					.addMouseListener(new java.awt.event.MouseAdapter() {
						public void mouseReleased(java.awt.event.MouseEvent e) {
							if (jLatitudeTextPane.getText().equals("")
									|| jLongitudeTextPane.getText().equals("")) {

							} else {
								try {
									Emulator.sendLocation(
											jLatitudeTextPane.getText(),
											jLongitudeTextPane.getText());
								} catch (SocketException e1) {
									e1.getMessage();
								} catch (IOException e1) {
									e1.getMessage();
								}
								closeFrame();
							}

						}
					});
		}
		return jSendLocationButton;
	}

	public void closeFrame() {
		this.setVisible(false);
	}
}
